<?php

class RegisterAction extends Action
{
    private $_description = array('-1'=>'信息不完整', '-2'=> '密码重复错误', '-3'=> '电话号码已存在', '-4'=> '数据库运行出错', '1'=>'注册成功');
    
    private $_userinfo;
    
	public function _onRegister(){
		
		$result = $this->checkPass();
		if($result == 1)
			$result = $this->checkMobile();
		if($result == 1)
			$result = $this->userAdd();
		if($result == 1)
			update_session($this->_userinfo);
		
		$_rdata = array('status'=> $result, 'msg'=>$this->_description[$result]);
		die(json_encode($_rdata));
	}
	
	private function checkPass(){
		return $_REQUEST['u_password'] != $_REQUEST['u_repassword'] ? -2 : 1 ;
	}
	
	private function checkMobile(){
		$_udata['u_mobile'] = $_REQUEST['u_mobile'];
		$result = M('User')->where($_udata)->find();
		return $result ? -3 : 1 ;
	}

	private function userAdd(){
		$_udata['u_name'] = $_REQUEST['u_name'];
		$_udata['u_password'] = md5($_REQUEST['u_password']);
		$_udata['u_realname'] = $_REQUEST['u_realname'];
		$_udata['u_mobile'] = $_REQUEST['u_mobile'];
		$_udata['u_liantong'] = $_REQUEST['u_liantong'];
		$_udata['u_address'] = $_REQUEST['u_address'];
		$_udata['u_ip'] = get_client_ip();
		$result = M('User')->data($_udata)->add();
		$this->_userinfo = M('User')->where(array('u_mobile'=>$_udata['u_mobile']))->find();
		return $this->_userinfo['u_id'] ? 1 : -4;
	}
		
	
	
	
}
?>